#include "et_additem.h"
#include "ui_et_additem.h"

ET_AddItem::ET_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ET_AddItem)
{
    ui->setupUi(this);

    table=TD_EXPRESS_TRACK;
}

ET_AddItem::~ET_AddItem()
{
    delete ui;
}

bool ET_AddItem::addItem(const QString &label,
                         const QString &detail,
                         const QString &addition)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (date_,label,detail,addition) "
                          "values (?,?,?,?)")
                  .arg(table));
    query.addBindValue(Today);
    query.addBindValue(label);
    query.addBindValue(detail);
    query.addBindValue(addition);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void ET_AddItem::on_btn_submit_clicked()
{
    QString label=ui->label->text().trimmed();
    QString express=ui->express->text().trimmed();
    QString ticket=ui->ticket->text().trimmed();
    QString detail = express + "#" + ticket;
    if(label.isEmpty() || detail.isEmpty())
        return;

    QString addition=ui->addition->text().trimmed();
    if(addItem(label,detail,addition))
        accept();
    else
        MESSAGE_CANNOT_SUBMIT
}
